<!--
 * Copyright 2022 The kubegems.io Authors
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
-->

<template>
  <div>
    <BaseSubTitle :title="$t('tip.config', ['elasticsearch'])" />
    <v-form ref="form" v-model="valid" lazy-validation>
      <v-row class="px-2 mt-0">
        <v-col cols="6">
          <v-text-field
            v-model="obj.spec.elasticsearch.host"
            class="my-0"
            label="host"
            required
            :rules="objRules.hostRules"
          />
        </v-col>
        <v-col cols="6">
          <v-text-field
            v-model="obj.spec.elasticsearch.port"
            class="my-0"
            label="port"
            required
            :rules="objRules.portRules"
          />
        </v-col>
        <v-col cols="12">
          <v-switch
            v-model="obj.spec.elasticsearch.logstash_format"
            hide-details
            label="logstash_format"
            @change="onLogstashFormatChange"
          />
        </v-col>
        <v-col v-if="!obj.spec.elasticsearch.logstash_format" cols="6">
          <v-text-field
            v-model.number="obj.spec.elasticsearch.index_name"
            class="my-0"
            label="index_name"
            required
            :rules="objRules.indexNameRules"
          />
        </v-col>
      </v-row>
    </v-form>
  </div>
</template>

<script>
  import { required } from '@kubegems/extension/ruler';

  import messages from '../../../../i18n';

  export default {
    name: 'Elasticsearch',
    i18n: {
      messages: messages,
    },
    props: {
      item: {
        type: Object,
        default: null,
      },
    },
    data() {
      return {
        valid: false,
        obj: {
          spec: {
            elasticsearch: {
              host: '',
              port: '',
              index_name: '',
              logstash_format: false,
              ssl_verify: false,
            },
          },
        },
        objRules: {
          hostRules: [required],
          portRules: [required],
          indexNameRules: [required],
        },
      };
    },
    mounted() {
      this.$nextTick(() => {
        this.$refs.form.resetValidation();
        if (this.item?.spec?.elasticsearch) {
          this.obj = this.item;
          this.$forceUpdate();
        }
      });
    },
    methods: {
      reset() {
        this.$refs.form.resetValidation();
        this.obj = this.$options.data().obj;
      },
      validate() {
        return this.$refs.form.validate(true);
      },
      onLogstashFormatChange() {
        if (this.obj.spec.elasticsearch.logstash_format) {
          this.obj.spec.elasticsearch.index_name = '';
        }
      },
    },
  };
</script>
